import java.util.Arrays;

public class PriorityQueue {
    public int[] elem;
    public int usedSize;

    public PriorityQueue() {
        this.elem=new int[10];
        this.usedSize=0;
    }
    public void createHeap(int[] array) {
        for(int parent=(usedSize-1)/2;parent>=0;parent--){
            shiftDown(parent,usedSize);
        }
    }
    private void shiftDown(int root,int len) {
        int child=root*2+1;
        while(child<len){
            if(child+1<usedSize&&elem[child]<elem[child+1]){
                child++;
            }
            if(elem[child]>elem[root]){
                swap(elem,child,root);
                root=child;
                child=2*root+1;
            }else{
               break;
            }
        }
    }
    public void swap(int[] array,int dest,int src){
        int tmp=array[dest];
        array[dest]=array[src];
        array[src]=tmp;
    }
    public void push(int val) {
        if(isFull()){
            elem= Arrays.copyOf(elem,elem.length*2);
        }
        this.elem[usedSize]=val;
        usedSize++;

    }

    private void shiftUp(int child) {
        int parent=(child-1)/2;
        while(child>0){
            if(elem[child]>elem[parent]){
                swap(elem,child,parent);
                child=parent;
                parent=(child-1)/2;
            }else{
                break;
            }
        }
    }

    public boolean isFull() {
        return usedSize==elem.length;
    }
    public void pollHeap() {
        if(isEmpty())return;
        swap(elem,0,usedSize-1);
        usedSize--;
        shiftDown(0,usedSize);
    }

    public boolean isEmpty() {
        return usedSize==0;
    }
    public int peekHeap() {
        return elem[0];
    }
}
